﻿using CNKI.TPI.Web.Search.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.SessionState;

namespace CNKI.TPI.Web.Base
{
    public enum PermissionOption
    {
        /// <summary>
        /// 过滤权限
        /// </summary>
        FilteringPermissions = 0,
        /// <summary>
        /// 不过滤权限
        /// </summary>
        None = 1
    };

    public class SessionHelper
    {
        public enum DBRightType
        {
            Manage = -1,
            Search = 1,
            Add = 2,
            Modify = 3,
            Delete = 4,
            Assign = 5,
            Mark = 6,
            Check = 8,
            Download = 9,
            View = 10,
            Print = 11,
            Copy = 12
        };
        /// <summary>
        /// 判断角色列表里面是否有管理员权限
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        private static bool IsRoleAdmin(LoginUserInfo userInfo)
        {
            bool m = false;
            IList<SYS_CMS_GROUP> list = userInfo.Role.Where(c => c.Type == -1).ToList();
            if (list != null && list.Count>0)
            {
                m = true;
            }
            return m;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static IList<int> GetUserDBRight()
        {
            IList<int> result = null;
            try
            {
                var session = System.Web.HttpContext.Current.Session;
                if (session != null && session["UserInfo"] != null)
                {
                    LoginUserInfo userInfo = (LoginUserInfo)session["UserInfo"];
                    if (IsRoleAdmin(userInfo) || null == userInfo.DBInfo)
                    {
                        result = null;
                    }
                    else
                    {
                        result = userInfo.DBInfo.Select(c => c.DBID).ToList<int>();
                        if (result.Count == 0)
                        {
                            result.Add(-1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbID"></param>
        /// <returns></returns>
        public static int GetUserOperateFunc(int dbID)
        {
            int result = 0;
            try
            {
                var session = System.Web.HttpContext.Current.Session;
                if (session != null && session["UserInfo"] != null)
                {
                    LoginUserInfo userInfo = (LoginUserInfo)session["UserInfo"];
                    if (IsRoleAdmin(userInfo) || null == userInfo.DBInfo)
                    {
                        result = -1;
                    }
                    else
                    {
                        IEnumerable<UserDBInfo> dbinfoList = userInfo.DBInfo.Where(c => c.DBID == dbID);
                        if (dbinfoList != null && dbinfoList.Count() > 0)
                        {
                            result = dbinfoList.First<UserDBInfo>().OperateFunc;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        public static int GetUserOperateFunc(string dbCode)
        {
            int result = 0;
            try
            {
                var session = System.Web.HttpContext.Current.Session;
                if (session != null && session["UserInfo"] != null)
                {
                    LoginUserInfo userInfo = (LoginUserInfo)session["UserInfo"];
                    if (IsRoleAdmin(userInfo) || null == userInfo.DBInfo)
                    {
                        result = -1;
                    }
                    else
                    {
                        IEnumerable<UserDBInfo> dbinfoList = userInfo.DBInfo.Where(c => c.DBCode == dbCode);
                        if (dbinfoList != null && dbinfoList.Count() > 0)
                        {
                            result = dbinfoList.First<UserDBInfo>().OperateFunc;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 检查当前用户是否有下载权限
        /// </summary>
        /// <returns></returns>
        public static bool HasDownloadRight(int dbID)
        {
            bool result = true;
            try
            {
                if (IsUserInfoExsit())
                {
                    var session = System.Web.HttpContext.Current.Session;
                    LoginUserInfo userInfo = (LoginUserInfo)session["UserInfo"];
                    if (IsRoleAdmin(userInfo) || null == userInfo.DBInfo)
                    {
                        result = true;
                    }
                    else
                    {
                        result = CheckValue(GetUserOperateFunc(dbID), DBRightType.Download);
                    }

                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 检查当前用户时候有在线浏览权限
        /// </summary>
        /// <returns></returns>
        public static bool HasBrowseRight(int dbID)
        {
            bool result = true;
            try
            {
                if (IsUserInfoExsit())
                {
                    var session = System.Web.HttpContext.Current.Session;
                    LoginUserInfo userInfo = (LoginUserInfo)session["UserInfo"];
                    if (IsRoleAdmin(userInfo) || null == userInfo.DBInfo)
                    {
                        result = true;
                    }
                    else
                    {
                        result = CheckValue(GetUserOperateFunc(dbID), DBRightType.View);
                    }

                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 检查当前用户时候有在线提交权限
        /// </summary>
        /// <returns></returns>
        public static bool HasSubmitRight(int dbID)
        {
            bool result = true;
            try
            {
                if (IsUserInfoExsit())
                {
                    var session = System.Web.HttpContext.Current.Session;
                    LoginUserInfo userInfo = (LoginUserInfo)session["UserInfo"];
                    if (IsRoleAdmin(userInfo) || null == userInfo.DBInfo)
                    {
                        result = true;
                    }
                    else
                    {
                        result = CheckValue(GetUserOperateFunc(dbID), DBRightType.Add);
                    }

                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 检查当前用户时候有在线提交权限
        /// </summary>
        /// <returns></returns>
        public static bool HasSubmitRight(string dbCode)
        {
            bool result = true;
            try
            {
                if (IsUserInfoExsit())
                {
                    var session = System.Web.HttpContext.Current.Session;
                    LoginUserInfo userInfo = (LoginUserInfo)session["UserInfo"];
                    if (IsRoleAdmin(userInfo) || null == userInfo.DBInfo)
                    {
                        result = true;
                    }
                    else
                    {
                        result = CheckValue(GetUserOperateFunc(dbCode), DBRightType.Add);
                    }

                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 检查当前用户时候有修改权限
        /// </summary>
        /// <returns></returns>
        public static bool HasModifyRight(int dbID)
        {
            bool result = true;
            try
            {
                if (IsUserInfoExsit())
                {
                    var session = System.Web.HttpContext.Current.Session;
                    LoginUserInfo userInfo = (LoginUserInfo)session["UserInfo"];
                    if (IsRoleAdmin(userInfo) || null == userInfo.DBInfo)
                    {
                        result = true;
                    }
                    else
                    {
                        result = CheckValue(GetUserOperateFunc(dbID), DBRightType.Modify);
                    }

                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 检查当前用户时候有删除权限
        /// </summary>
        /// <returns></returns>
        public static bool HasDeleteRight(int dbID)
        {
            bool result = true;
            try
            {
                if (IsUserInfoExsit())
                {
                    var session = System.Web.HttpContext.Current.Session;
                    LoginUserInfo userInfo = (LoginUserInfo)session["UserInfo"];
                    if (IsRoleAdmin(userInfo) || null == userInfo.DBInfo)
                    {
                        result = true;
                    }
                    else
                    {
                        result = CheckValue(GetUserOperateFunc(dbID), DBRightType.Delete);
                    }

                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 检查当前用户时候有审核权限
        /// </summary>
        /// <returns></returns>
        public static bool HasCheckRight(int dbID)
        {
            bool result = true;
            try
            {
                if (IsUserInfoExsit())
                {
                    var session = System.Web.HttpContext.Current.Session;
                    LoginUserInfo userInfo = (LoginUserInfo)session["UserInfo"];
                    if (IsRoleAdmin(userInfo) || null == userInfo.DBInfo)
                    {
                        result = true;
                    }
                    else
                    {
                        result = CheckValue(GetUserOperateFunc(dbID), DBRightType.Check);
                    }

                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        private static bool CheckValue(int value, DBRightType checkIndex)
        {
            bool result = false;
            try
            {
                if (-1 == value)
                {
                    result = true;
                }
                else
                {
                    string band2 = Convert.ToString(value, 2);
                    if (band2.Length >= (int)checkIndex && "1".Equals(band2.Substring(band2.Length - (int)checkIndex, 1)))
                    {
                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        public static double BuildDBRight(IList<DBRightType> dbRightList)
        {
            int result = -1;
            if (!dbRightList.Contains(DBRightType.Manage))
            {
                result = 0;
                foreach (DBRightType dBRightType in dbRightList)
                {
                    result += Convert.ToInt32(Math.Pow(2, ((int)dBRightType - 1)));
                }
            }
            return result;
        }

        public static IList<DBRightType> GetAllDBRight()
        {
            IList<DBRightType> result = new List<DBRightType>();
            try
            {
                result.Add(DBRightType.Manage);
                Type rightType = typeof(DBRightType);
                foreach (int item in Enum.GetValues(rightType))
                {
                    if (-1 == item)
                    {
                        continue;
                    }
                    result.Add((DBRightType)item);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        public static IList<DBRightType> PaserDBRight(int rightValue)
        {
            IList<DBRightType> result = new List<DBRightType>();
            try
            {
                Type rightType = typeof(DBRightType);
                foreach (int item in Enum.GetValues(rightType))
                {
                    if (-1 == rightValue)
                    {
                        result.Add(DBRightType.Manage);
                        break;
                    }
                    if (CheckValue(rightValue, (DBRightType)item))
                    {
                        result.Add((DBRightType)item);
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        public static string GetDBRightDesc(int rightValue)
        {
            string result = "";
            IList<DBRightType> rightList = PaserDBRight(rightValue);
            foreach (DBRightType item in rightList)
            {
                result += GetDBRightDesc(item) + ",";
            }
            return result.Trim(',');
        }

        public static string GetDBRightDesc(DBRightType rightType)
        {
            string result = null;
            switch (rightType)
            {
                case DBRightType.Manage:
                    result = "管理";
                    break;
                case DBRightType.Search:
                    result = "检索";
                    break;
                case DBRightType.Add:
                    result = "添加";
                    break;
                case DBRightType.Modify:
                    result = "修改";
                    break;
                case DBRightType.Delete:
                    result = "删除";
                    break;
                case DBRightType.Assign:
                    result = "分配任务";
                    break;
                case DBRightType.Mark:
                    result = "标引";
                    break;
                case DBRightType.Check:
                    result = "检查";
                    break;
                case DBRightType.Download:
                    result = "文件下载";
                    break;
                case DBRightType.View:
                    result = "在线浏览";
                    break;
                case DBRightType.Print:
                    result = "文件打印";
                    break;
                case DBRightType.Copy:
                    result = "文件拷贝";
                    break;
                default:
                    break;
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbID"></param>
        /// <returns></returns>
        public static IList<string> GetUserField(int dbID)
        {
            IList<string> result = null;
            try
            {
                var session = System.Web.HttpContext.Current.Session;
                if (session != null && session["UserInfo"] != null)
                {
                    LoginUserInfo userInfo = (LoginUserInfo)session["UserInfo"];
                    if (IsRoleAdmin(userInfo) || null == userInfo.DBInfo)
                    {
                        result = null;
                    }
                    else
                    {
                        IEnumerable<UserDBInfo> dbinfoList = userInfo.DBInfo.Where(c => c.DBID == dbID);
                        if (dbinfoList != null && dbinfoList.Count() > 0)
                        {
                            result = dbinfoList.First<UserDBInfo>().Field;
                            if (result != null && result.Count == 0)
                            {
                                result = new List<string>() { "-1" };
                            }
                        }
                        else
                        {
                            result = new List<string>() { "-1" };
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbID"></param>
        /// <returns></returns>
        public static string GetUserCLS(int dbID)
        {
            string result = null;
            try
            {
                var session = System.Web.HttpContext.Current.Session;
                if (session != null && session["UserInfo"] != null)
                {
                    LoginUserInfo userInfo = (LoginUserInfo)session["UserInfo"];
                    if (IsRoleAdmin(userInfo) || null == userInfo.DBInfo)
                    {
                        result = null;
                    }
                    else
                    {
                        IEnumerable<UserDBInfo> dbinfoList = userInfo.DBInfo.Where(c => c.DBID == dbID);
                        if (dbinfoList != null && dbinfoList.Count() > 0)
                        {
                            result = dbinfoList.First<UserDBInfo>().Navi;
                            if ("".Equals(result))
                            {
                                result = "";
                            }
                        }
                        else
                        {
                            result = "";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool IsUserInfoExsit()
        {
            bool result = false;
            try
            {
                HttpSessionState session = null;
                if (System.Web.HttpContext.Current != null)
                {
                    session = System.Web.HttpContext.Current.Session;
                }

                if (null != session && null != session["UserInfo"])
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static IList<string> GetUserSecurity()
        {
            IList<string> result = new List<string>();
            try
            {
                var session = System.Web.HttpContext.Current.Session;
                if (session != null && session["UserInfo"] != null)
                {
                    LoginUserInfo userInfo = (LoginUserInfo)session["UserInfo"];
                    if (IsRoleAdmin(userInfo)|| null == userInfo.DBInfo)
                    {
                        result = null;
                    }
                    else
                    {
                        result = userInfo.Security;
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        public static LoginUserInfo GetUserInfo()
        {
            LoginUserInfo result = null;
            try
            {
                var session = System.Web.HttpContext.Current.Session;
                if (session != null && session["UserInfo"] != null)
                {
                    result = (LoginUserInfo)session["UserInfo"];
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        public static void SetUserInfo(LoginUserInfo userInfo)
        {
            try
            {
                var session = System.Web.HttpContext.Current.Session;
                session["UserInfo"] = userInfo;
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return;
        }

        public static void ClearUserInfo()
        {
            try
            {
                if (IsUserInfoExsit())
                {
                    var session = System.Web.HttpContext.Current.Session;
                    session.Remove("UserInfo");
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return;
        }

    }
}
